import { Component, OnInit } from '@angular/core';
import { Storage } from '@ionic/storage';
import {NgbActiveModal} from '@ng-bootstrap/ng-bootstrap';
import {AuthService} from '../../services/auth.service';
@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
  username:string;
  password:string;

  constructor(private authservice:AuthService
    ,public activeModal: NgbActiveModal
    ,private storage: Storage) { }


  ngOnInit(): void {
  }


  login(){

    this.authservice.login(this.username,this.password)
      .then((data:any)=>{
        if(data.msg=='ok'){
          this.storage.set("TOKEN",data.token)  //存储token到浏览器本地存储中
            .then(()=>{
              this.storage.set("FRESHTIME",new Date().getTime());
              this.activeModal.close('success');//关闭登录框
            })
        }
        else {
          alert('用户名或是密码错误');
        }
      })

  }
  cancel(){
    this.activeModal.close('cancel');
  }

  regist(){
    this.activeModal.close('regist');

  }


}
